@extends('layouts.app')

@section('content')


<script src="{{ URL::asset('/') }}earth/js/echarts.min.js"></script>


    <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
    <div style="position: relative;width: 1200px;" >
    	<button class="btn btn-default" id="back" style="position: absolute;top: 10px;right: 0px;z-index: 10;">返回</button>
	    <div id="main" style="width: 1200px;height:800px;">
    </div>

    </div>
    <script type="text/javascript">

    	$("#back").click(function(){
    		myChart.clear();
    		load_map_data('湖北省');
    	})

		// 全省地图数据

		var hubei_data = [
							{city:'湖北省', map_name:'HB', map:"{{ URL::asset('/') }}earth/map_data/hubei.js"},
							{city:'武汉市', map_name:'WH', map:"{{ URL::asset('/') }}earth/map_data/wuhan.json"},
							{city:'荆州市', map_name:'JZ', map:"{{ URL::asset('/') }}earth/map_data/jingzhou.json"},
							{city:'恩施土家族苗族自治州', map_name:'ES', map:"{{ URL::asset('/') }}earth/map_data/enshi.json"},
							{city:'鄂州市', map_name:'EZ', map:"{{ URL::asset('/') }}earth/map_data/ezhou.json"},
							{city:'黄冈市', map_name:'HG', map:"{{ URL::asset('/') }}earth/map_data/huanggang.json"},
							{city:'黄石市', map_name:'HS', map:"{{ URL::asset('/') }}earth/map_data/huangshi.json"},
							{city:'荆门市', map_name:'JM', map:"{{ URL::asset('/') }}earth/map_data/jingmen.json"},
							{city:'十堰市', map_name:'SY', map:"{{ URL::asset('/') }}earth/map_data/shiyan.json"},
							{city:'随州市', map_name:'SZ', map:"{{ URL::asset('/') }}earth/map_data/suizhou.json"},
							{city:'襄阳市', map_name:'XY', map:"{{ URL::asset('/') }}earth/map_data/xiangyang.json"},
							{city:'咸宁市', map_name:'XN', map:"{{ URL::asset('/') }}earth/map_data/xianning.json"},
							{city:'孝感市', map_name:'XG', map:"{{ URL::asset('/') }}earth/map_data/xiaogan.json"},
							{city:'宜昌市', map_name:'YC', map:"{{ URL::asset('/') }}earth/map_data/yichang.json"},
							{city:'神农架林区', map_name:'SNJ', map:"{{ URL::asset('/') }}earth/map_data/shennongjia.json"},
							{city:'潜江市', map_name:'QJ', map:"{{ URL::asset('/') }}earth/map_data/qianjiang.json"},
							{city:'天门市', map_name:'TM', map:"{{ URL::asset('/') }}earth/map_data/tianmen.json"},
							{city:'仙桃市', map_name:'TM', map:"{{ URL::asset('/') }}earth/map_data/xiantao.json"},
						];

		// 地图加载数据，默认未加载任何数据
		var map_load = [];

		//地区统计数据
		var data_arr = [];

		// 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));

		// 当前地图数据
		var uploadedDataURL = "{{ URL::asset('/') }}earth/map_data/hubei.js";

		// 定义单击事件
		myChart.on('click', function (params) {
			console.log(params.name);
			load_map_data(params.name);
		});

		// 找到地图数据
		function get_map_data(name){
			for (i = 0; i < hubei_data.length; i++)
			{
				if (hubei_data[i].city == name)
				{
					return hubei_data[i];
				}
			}
		}


		// 加载地图数据
		function load_map_data(name)
		{
			console.log(name);
			var cityname = '';
			cityname = name;
			var map = get_map_data(name);
			if (map == null){
				console.log('no map data');
			} else {
				uploadedDataURL = map.map;
				if(cityname == "湖北省"){
					cityname = "1";
				}else if(cityname == "恩施土家族苗族自治州"){
					cityname = "恩施市";
				}else if(cityname == "神农架林区"){
					cityname = "神农架";
				}
				$.ajax({
					type:"POST",
					url:"{{url('admin/Earth/tjdw')}}",
					data: {'name':cityname, '_token':'{{csrf_token()}}'},
					async:true,
					success:function(data){
						var obj = jQuery.parseJSON(data)
						console.log(obj);
						data_arr = obj.result;

            		// data 为城市的数据
    				//data_arr = [{name: '武汉市', value: 20057.34}, {name: '荆州市', value: 15477.48}];
    				load_chart(map, data_arr);
					}
				});
			}
		}


		function load_chart(map){
			console.log("map");
			console.log(map);
			// 最好写入过滤的方法，不要重复加载地图
			$.getJSON(uploadedDataURL, function (geoJson) {

				myChart.hideLoading();

				echarts.registerMap(map.map_name, geoJson);

				myChart.setOption(option = {
					title: {
						text: map.city + '数据',
						subtext: '内保企业数据',
						sublink: 'http://nb.yntosite.com'
					},
					tooltip: {
						trigger: 'item',
						formatter: '{c} '
					},
					toolbox: {
						show: false,
						orient: 'vertical',
						left: 'right',
						top: 'center',
						feature: {
							dataView: {readOnly: false},
							restore: {},
							saveAsImage: {}
						}
					},
					visualMap: {
						min: 800,
						max: 50000,
						text:['High','Low'],
						realtime: false,
						calculable: true,
						color: ['orangered','yellow','lightskyblue']
					},
					series: [
						{
							name: map.city,
							type: 'map',
							mapType: map.map_name, // 自定义扩展图表类型
							itemStyle:{
								normal:{label:{show:true}},
								emphasis:{label:{show:true}}
							},
							data:data_arr
						}
					]
				});
			});
		}


		// 开始加载湖北地图
		load_map_data('湖北省');

		//load_hb();
	</script>
@stop
